package collector.app.nexd.com.singlecollection.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;

import collector.app.nexd.com.singlecollection.MainActivity;
import collector.app.nexd.com.singlecollection.R;
import collector.app.nexd.com.singlecollection.adapter.CityAdapter;
import collector.app.nexd.com.singlecollection.adapter.MallAdapter;
import collector.app.nexd.com.singlecollection.model.Mall;
import collector.app.nexd.com.singlecollection.presenter.building.LoadBuildingContract;
import collector.app.nexd.com.singlecollection.presenter.building.LoadBuildingPresenter;
import collector.app.nexd.com.singlecollection.presenter.db.TableContract;
import collector.app.nexd.com.singlecollection.util.ProgressUtil;
import collector.app.nexd.com.singlecollection.widget.CustomRecyclerView;
import collector.app.nexd.com.singlecollection.widget.DividerItemDecoration;
import collector.app.nexd.com.singlecollection.widget.OnRecyclerViewItemClickListener;

public class SelectBuildingActivity extends AppCompatActivity implements LoadBuildingContract.View, View.OnClickListener {
    private static final String TAG = "SelectBuildingActivity";
    private Toolbar tlBar;
    private ImageView ivSetting;
    private LoadBuildingContract.Presenter loadBuildingPresenter;
    private ProgressDialog pgLoading;
    private CustomRecyclerView rlCity, rlBuilding;
    private CityAdapter cityAdapter;
    private MallAdapter mallAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_buliding);
        loadBuildingPresenter = new LoadBuildingPresenter(this);
        initView();
        bindData();
        loadMall();
    }

    private void loadMall() {
        cityAdapter.setItemClickListener(new OnRecyclerViewItemClickListener() {

                                             @Override
                                             public void onClick(int position, RecyclerView.ViewHolder holder) {
                                                 List<Mall> malls = loadBuildingPresenter.loadMalls(((CityAdapter.ViewHolder) holder).getCityName());
                                                 mallAdapter.setMalls(malls);
                                             }
                                         }
        );
        mallAdapter.setItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onClick(int position, RecyclerView.ViewHolder holder) {
                Mall selectedMall = mallAdapter.getMalls().get(position);
                Intent intent = new Intent(SelectBuildingActivity.this, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(TableContract.Building.COLUMN_CITY_NAME, selectedMall.getCnCityName());
                bundle.putString(TableContract.Building.COLUMN_CITY_NAME_PY, selectedMall.getPyCityName());
                bundle.putString(TableContract.Building.COLUMN_BUILDING_NAME, selectedMall.getCnName());
                bundle.putString(TableContract.Building.COLUMN_BUILDING_NAME_PY, selectedMall.getPyName());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadBuildingPresenter.init();
    }

    private void bindData() {
        rlBuilding.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        rlCity.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        rlCity.setEmptyView(findViewById(R.id.empty_layout));
        rlBuilding.setEmptyView(findViewById(R.id.empty_layout));
        cityAdapter = new CityAdapter();
        rlCity.setAdapter(cityAdapter);
        mallAdapter = new MallAdapter();
        rlBuilding.setAdapter(mallAdapter);

    }


    @Override
    public void initView() {
        rlCity = (CustomRecyclerView) findViewById(R.id.building_rl_city);
        rlCity.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rlBuilding = (CustomRecyclerView) findViewById(R.id.building_rl_mall);
        rlBuilding.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        tlBar = (Toolbar) findViewById(R.id.building_tl);
        ivSetting = (ImageView) findViewById(R.id.building_iv_setting);
        pgLoading = ProgressUtil.createProgressDialog(this);
        setSupportActionBar(tlBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        ivSetting.setOnClickListener(this);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(SelectBuildingActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void printLog(String msg) {
        Log.d(TAG, "printLog: " + msg);
    }

    @Override
    public void loading() {
        pgLoading.show();
    }

    @Override
    public void loadDataSuccess() {
        cityAdapter.resetSelection();
        showToast("数据load成功");
    }

    @Override
    public void loadDataFail() {

    }


    @Override
    public void querySuccess(List<Map<String, Object>> cityList, List<Mall> malls) {
        cityAdapter.setCities(cityList);
        mallAdapter.setMalls(malls);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.building_iv_setting:
                loadBuildingPresenter.loadData();
                break;
        }
    }


}
